package com.meixiang.activity.homes.cosmeticTraining;

import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.service.ServiceEvaluateActivity;
import com.meixiang.activity.homes.service.ShowPictureActivity;
import com.meixiang.activity.homes.service.SubmitOrdersActivity;
import com.meixiang.adapter.home.CosmeticTrainingCourseAdapter;
import com.meixiang.entity.training.TrainingDetails;
import com.meixiang.global.Config;
import com.meixiang.global.GlobalType;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;

/**
 * @author yinsujun  Created at 2016/5/12 14:39
 *        美容培训之课程培训详情
 */
public class CosmeticTrainingDetailActivity extends BaseActivity {
    @Bind(R.id.title)
    TitleView titleView;
    @Bind(R.id.training_detail_iv_banner)
    ImageView ivBanner;
    @Bind(R.id.training_detail_tv_title)
    TextView tvTitle;
    @Bind(R.id.training_detail_tv_now_price)
    TextView tvNowPrice;
    @Bind(R.id.training_detail_tv_people_apply)
    TextView tvPeopleApply;
    @Bind(R.id.training_detail_tv_introduce_content)
    TextView tvIntroduceContent;
    @Bind(R.id.training_detail_tv_course_evaluate)
    TextView tvCourseEvaluate;
    @Bind(R.id.training_detail_tv_merchant_information)
    TextView tvMerchantInformation;
    @Bind(R.id.training_detail_tv_merchant_content)
    TextView tvMerchantContent;
    @Bind(R.id.training_detail_view_tel)
    View trainingDetailViewTel;
    @Bind(R.id.training_detail_iv_tel)
    ImageView trainingDetailIvTel;
    @Bind(R.id.training_detail_relative_merchant_information)
    RelativeLayout relativeMerchantInformation;
    @Bind(R.id.training_detail_bt_confirm)
    Button btConfirm;
    @Bind(R.id.training_detail_linear_confirm)
    LinearLayout linearConfirm;
    @Bind(R.id.training_detail_tv_outmoded_price)
    TextView tvOutmodedPrice;

    @Bind(R.id.training_detail_recycler_view)
    RecyclerView mRecyclerView;

    private String goodsId;
    private CosmeticTrainingCourseAdapter mAdapter;
    private TrainingDetails trainingDetail;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cosmetic_training_detail);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle("培训详情", "", "", R.mipmap.ic_training_service, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v.equals(titleView.getLeftTitleView())) {
                    CosmeticTrainingDetailActivity.this.finish();
                } else {
                    if (RongIM.getInstance() != null)
                        if(trainingDetail!=null&&CosmeticTrainingDetailActivity.this.isFinishing()==false){
                                                      //1 是总店 0不是总店
                            if(trainingDetail.getIsMainStore().equals("1")){
                                Tool.SelectCustomerService(context,true);
                            }else{
                                Tool.SelectCustomerService(context,false);
                            }

                        }


                    }
            }
        });
    }



    @Override
    protected void initView() {
        goodsId=getIntent().getStringExtra("goodsId");
        tvOutmodedPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter=new CosmeticTrainingCourseAdapter();
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    protected void setData() {
        getData();

    }
    private void getData(){
        HttpUtils.isAnalog=false;
        String url= Config.TRAINING_FOR_GET_DETAILS;
        HttpParams params=new HttpParams();
        params.put("goodsId",goodsId);
//        JsonBean resultData = VirtualDataHelper.getResultData(context, "floor/goodsDetail.json");
        HttpUtils.post(url, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                TrainingDetails trainingDetails= AbJsonUtil.fromJson(result.toString(),TrainingDetails.class);
                trainingDetail=trainingDetails;
                setDetail(trainingDetails);
            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(context,message);
            }
        });
//        TrainingDetails trainingDetails= AbJsonUtil.fromJson(resultData.getResult(),TrainingDetails.class);
//        setDetail(trainingDetails);
    }
    private void setDetail(TrainingDetails trainingDetails){
        GlideHelper.showImage(context,trainingDetails.getGoodsImage(),ivBanner);
        final ArrayList<String> urls=new ArrayList<>();
        urls.add(trainingDetails.getGoodsImage());
        ivBanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, ShowPictureActivity.class);
                intent.putStringArrayListExtra("urls", urls);
                startActivity(intent);
            }
        });
        tvTitle.setText(trainingDetails.getGoodsName());
        tvNowPrice.setText(trainingDetails.getGoodsStorePrice());
        tvOutmodedPrice.setText(trainingDetails.getGoodsMarketPrice());
        //报名人数
        tvPeopleApply.setText(trainingDetails.getSalenum()+"人报名");
        //详细内容
        tvIntroduceContent.setText(trainingDetails.getGoodsBody());
        //商家信息
        tvMerchantContent.setText(trainingDetails.getAddress());
        mAdapter.addAll(trainingDetails.getInstructionList());

    }

    @OnClick({R.id.training_detail_tv_course_evaluate, R.id.training_detail_bt_confirm,R.id.training_detail_iv_tel})
    void click(View view) {
        Intent intent = new Intent();
        if (view.equals(tvCourseEvaluate)) {
            intent.setClass(context, ServiceEvaluateActivity.class);
            if(trainingDetail!=null){
                intent.putExtra("o2oTrainId",trainingDetail.getGoodsId());
                intent.putExtra("type",GlobalType.TRAIN_EVALUATE_TYPE);
            }else{
                Tool.showTextToast(context,"未获取到商品号");
                return;
            }
        } else if (view.equals(btConfirm)) {
            if(trainingDetail!=null){
                intent.setClass(context, SubmitOrdersActivity.class);
                intent.putExtra("type", GlobalType.TRAIN_TYPE);
                intent.putExtra("title",trainingDetail.getGoodsName());
                intent.putExtra("goodsBody",trainingDetail.getGoodsBody());
                intent.putExtra("price",trainingDetail.getGoodsStorePrice());
                intent.putExtra("goodsImage",trainingDetail.getGoodsImage());
                intent.putExtra("goodsId",trainingDetail.getGoodsId());
                intent.putExtra("isMainStore", trainingDetail.getIsMainStore());
            }else{
                Tool.showTextToast(context,"获取详情失败");
                return;
            }
        }else if(view.equals(trainingDetailIvTel)){
            if(trainingDetail!=null&&!AbStrUtil.isEmpty(trainingDetail.getPhone())){
                Intent intentPhone = new Intent(Intent.ACTION_DIAL);
                intentPhone.setData(Uri.parse("tel:" + trainingDetail.getPhone()));
                if (intentPhone.resolveActivity(getPackageManager()) != null) {
                    startActivity(intentPhone);
                }
                return;
            }else{
                return;
            }
        }
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {

    }
}
